/*
 * File:         DestinationTest.java
 * Author:       Robert Bittle <guywithnose@gmail.com>
 */
package triperest.tests;

import static org.junit.Assert.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import tests.AppEngineTest;
import triperest.Destination;
import triperest.Review;

/**
 * The Class DestinationTest.
 */
public class DestinationTest extends AppEngineTest {

	/** The destination. */
	private Destination destination;
	
	/** The Id. */
	private long Id = 0;
	
	/* (non-Javadoc)
	 * @see tests.AppEngineTest#setUp()
	 */
	@Before
	@Override
	public void setUp()
	{
		super.setUp();
		destination = new Destination();
		destination.setName("testName");
		destination.setRank(5);
		destination.setPlacesId("the place id");
	}
	
	/* (non-Javadoc)
	 * @see tests.AppEngineTest#tearDown()
	 */
	@Override
	@After
	public void tearDown()
	{
		if(destination.getID() != 0)
			destination.delete();
		super.tearDown();
	}
	
	/**
	 * Tests the save method using insert.
	 */
	@Test
	public void testSave_Insert() {
		destination.save();
		Id = destination.getID();
		Destination destination2 = new Destination(Id);
		destination2.load();
		assertEquals(destination.getName(), destination2.getName());
		assertEquals(destination.getPlacesId(), destination2.getPlacesId());
		assertEquals(destination.getRank(), destination2.getRank());
	}
	
	/**
	 * Tests the save method using update.
	 */
	@Test
	public void testSave_Update() {
		destination.save();
		Id = destination.getID();
		destination.setName("Another Test Name");
		destination.setPlacesId("Another PID");
		destination.setRank(32);
		destination.save();
		Destination destination2 = new Destination(Id);
		destination2.load();
		assertEquals(destination.getName(), destination2.getName());
		assertEquals(destination.getPlacesId(), destination2.getPlacesId());
		assertEquals(destination.getRank(), destination2.getRank());
	}
	
	/**
	 * Tests the delete method.
	 */
	@Test
	public void testDelete() {
		destination.save();
		assertTrue(destination.load());
		destination.delete();
		assertFalse(destination.load());
	}
	
	/**
	 * Tests the save method using insert reviews.
	 */
	@Test
	public void testSave_InsertReviews() {
		Review review = new Review();
		review.userId = "A test User Id";
		review.rank = 7;
		review.comment = "My test comment";
		destination.addReview(review);
		destination.save();
		Destination destination2 = new Destination(destination.getID());
		destination2.load();
		assertEquals(destination.getName(), destination2.getName());
		assertEquals(destination.getPlacesId(), destination2.getPlacesId());
		assertEquals(destination.getRank(), destination2.getRank());
		assertEquals(destination.getReviews().get(0).userId, destination2.getReviews().get(0).userId);
		assertEquals(destination.getReviews().get(0).rank, destination2.getReviews().get(0).rank);
		assertEquals(destination.getReviews().get(0).comment, destination2.getReviews().get(0).comment);
		review.delete();
	}
	
	/**
	 * Tests the save method using update reviews.
	 */
	@Test
	public void testSave_UpdateReviews() {
		destination.save();
		Review review = new Review();
		review.userId = "A test User Id";
		review.rank = 7;
		review.comment = "My test comment";
		destination.addReview(review);
		destination.save();
		destination.getReviews().get(0).userId = "Another User Id";
		destination.getReviews().get(0).rank = 9;
		destination.getReviews().get(0).comment = "Another test comment";
		destination.save();
		Destination destination2 = new Destination(destination.getID());
		destination2.load();
		assertEquals(destination.getName(), destination2.getName());
		assertEquals(destination.getPlacesId(), destination2.getPlacesId());
		assertEquals(destination.getRank(), destination2.getRank());
		assertEquals(destination.getReviews().get(0).userId, destination2.getReviews().get(0).userId);
		assertEquals(destination.getReviews().get(0).rank, destination2.getReviews().get(0).rank);
		assertEquals(destination.getReviews().get(0).comment, destination2.getReviews().get(0).comment);
		review.delete();
	}
	
	/**
	 * Tests the getAllDestinations method.
	 */
	@Test
	public void testGetAllDestinations() {
		destination.setName("this is the text we are looking for");
		destination.save();
		assertTrue(Destination.getAllDestinations().contains("this is the text we are looking for"));
	}

}
